<template>
  <div class="app-container code-box">
    <el-row>
      <el-col :span="24" :xs="24">
        <el-form
          :model="queryParams"
          ref="queryForm"
          :inline="true"
          label-width="68px"
          style="margin-bottom: 10px"
        >
          <el-form-item label="业务名称" prop="status">
            <el-select
              v-model="queryParams.workType"
              clearable
              size="small"
              style="width: 200px"
              @click.native="getWorkTypeParent()"
            >
              <el-option
                v-for="dict in optionsworkType"
                :key="dict.dictType"
                :label="dict.remark"
                :value="dict.dictType"
              />
            </el-select>
          </el-form-item>

          <el-form-item>
            <el-button
              type="primary"
              icon="el-icon-search"
              size="mini"
              @click="handleQuery"
              v-hasPermi="['system:code:query']"
              >搜索</el-button
            >
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
              >重置</el-button
            >
          </el-form-item>
        </el-form>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
              v-hasPermi="['system:code:add']"
              >新增</el-button
            >
          </el-col>
        </el-row>
        <el-table v-loading="loading" :data="codeList" :height="maxHeight">
          <el-table-column label="排序" align="center" prop="orderNum" />
          <el-table-column label="业务名称" align="center" prop="remark" />
          <el-table-column
            label="业务类型"
            align="center"
            prop="workTypeName"
          ></el-table-column>
          <el-table-column label="编码前缀" align="center" prop="prefix" />
          <el-table-column label="自增值" align="center" prop="incNumber" />
          <el-table-column label="自增宽度" align="center" prop="incLen" />
          <el-table-column label="扩展日期" align="center" prop="exactDate">
            <template slot-scope="scope">
              <span>{{ scope.row.exactDate | getDateStatus }}</span>
            </template>
          </el-table-column>
          <el-table-column label="类型共享" align="center" prop="typeShare">
            <template slot-scope="scope">
              <span>{{ scope.row.typeShare | getShareStatus }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="创建时间"
            align="center"
            prop="createTime"
            width="180"
          >
            <template slot-scope="scope">
              <span>{{ parseTime(new Date(scope.row.createTime)) }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            align="center"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['system:code:edit']"
                >修改</el-button
              >
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['system:code:remove']"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </el-col>
    </el-row>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 添加或修改参数配置对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="600px"
      append-to-body
      @close="close"
    >
      <el-form
        :key="keyForm"
        ref="form"
        :model="form"
        :rules="rules"
        label-width="80px"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item label="业务名称" prop="workType">
              <el-select
                v-model="form.workType"
                @click.native="getWorkTypeParent()"
                @change="workTypeChange"
              >
                <el-option
                  v-for="dict in optionsworkType"
                  :key="dict.dictType"
                  :label="dict.remark"
                  :value="dict.dictType"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="业务类型" prop="workTypeId">
              <el-select
                v-model="form.workTypeId"
                clearable
                :disabled="form.workType == ''"
              >
                <el-option
                  v-for="dict in optionsworkTypeId"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="自增值" prop="incNumber">
              <el-input v-model="form.incNumber" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="自增宽度" prop="incLen">
              <el-input v-model="form.incLen" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="编码前缀">
              <el-input v-model="form.prefix" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="扩展日期" prop="exactDate">
              <el-switch
                v-model="form.exactDate"
                active-value="1"
                inactive-value="0"
              ></el-switch>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="排序" prop="orderNum">
              <el-input v-model="form.orderNum" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="类型共享" prop="exactDate">
              <el-switch
                v-model="form.typeShare"
                active-value="1"
                inactive-value="0"
              ></el-switch>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listCode,
  listWorkType,
  childWorkType,
  addCode,
  updateCode,
  delCode,
} from "@/api/system/code";

export default {
  name: "Code",
  data() {
    // 业务类型
    var checkValue = (rule, value, callback) => {
      if (this.value.length == 0) {
        return callback(new Error(""));
      } else {
        callback();
      }
    };

    return {
      // 遮罩层
      loading: true,
      // 表单数据
      form: {
        workTypeId: "",
        workType: "",
        coderId: "",
        typeShare: "0",
        exactDate: "0",
      },
     
      // 总条数
      total: 0,
      // 数据
      codeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 业务类型
      optionsworkType: [],
      //业务类型Id
      optionsworkTypeId: [],
      keyForm: "",
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 100,
        workType: "",
      },
      //字典数据最大高度
      maxHeight: null,
      //按钮状态
      butChange: "",
      // 表单校验
      rules: {
        workType: [
          {
            required: true,
            trigger: "change",
          },
        ],
        incNumber: [
          {
            required: true,
            trigger: "blur",
          },
        ],
        orderNum: [
          {
            required: true,
            trigger: "blur",
          },
        ],
        incLen: [
          {
            required: true,
            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {

    //查询单据编号列表
    getList() {
      this.loading = true;
      listCode(this.queryParams).then((response) => {
        this.total = response.total;
        this.loading = false;
        this.codeList = response.rows;
        if (this.codeList.length >= 13) {
          //条数总数大于显示出现滚动条
          this.maxHeight = `${document.documentElement.clientHeight}` - 270;
        } else {
          this.maxHeight = "";
        }
      });
    },

    //选择业务类型
    workTypeChange(value) {
      this.optionsworkTypeId = [];
      this.form.workTypeId = "";
      this.getWorkTypeChild(value);
    },

    /* 获取业务类型大类 */
    getWorkTypeParent() {
      return new Promise((resolve) => {
        listWorkType({ dictName: "业务类型" }).then((res) => {
          if (res.code == 200) {
            this.optionsworkType = res.rows;
            resolve(res.rows);
          }
        });
      });
    },

    /* 获取业务类型子类*/
    getWorkTypeChild(type) {
      this.optionsworkTypeId = [];
      return new Promise((resolve) => {
        childWorkType({ dictType: type }).then((res) => {
          if (res.code == 200) {
            this.optionsworkTypeId = res.rows;
            resolve(res.rows);
          }
        });
      });
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.workType = "";
      this.handleQuery();
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.butChange = "1";
      this.open = true;
      this.title = "新增";
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.keyForm++;
      this.form = JSON.parse(JSON.stringify(row));
      this.title = "修改";
      this.butChange = "2";
      this.getWorkTypeParent().then(() => {
        this.getWorkTypeChild(row.workType).then(() => {
          this.open = true;
        });
      });
    },

    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.coderId != "") {
            updateCode(this.form).then((response) => {
              if (response.code == 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addCode(this.form).then((response) => {
              if (response.code == 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
                this.$refs.form.resetFields(); //清除表单验证
              }
            });
          }
        }
      });
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      this.$confirm("确定删除?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return delCode(row.coderId);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        });
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    close() {
      this.open = false;
      this.reset();
    },

    // 表单重置
    reset() {
      this.form = {
        coderId: "",
        workTypeId: "",
        workType: "",
        exactDate: "0",
        typeShare: "0",
      };
      this.value = [];
      this.keyForm = 0;
      this.resetSelector();
      this.resetForm("form");
    },
    //解决级联选择器回显高亮异常问题
    resetSelector() {
      if (this.$refs.classTree) {
        this.$refs.classTree.$refs.panel.activePath = [];
        this.$refs.classTree.$refs.panel.calculateCheckedNodePaths();
      }
    },
  },

  filters: {
    getDateStatus(status) {
      switch (status) {
        case "0":
          return "不启用";
          break;
        case "1":
          return "启用";
          break;

        default:
          return "不启用";
      }
    },
    getShareStatus(status) {
      switch (status) {
        case "0":
          return "不共享";
          break;
        case "1":
          return "共享";
          break;

        default:
          return "不共享";
      }
    },
  },
};
</script>
<style lang="scss">
.dick-select {
  input {
    color: #1890ff !important;
  }
}
.el-icon-loading {
  display: none;
}
.code-box {
  .none-data li {
    color: #999;
    justify-content: center;
    padding-top: 10px !important;
  }
  .dict-module {
    .dic-t {
      margin-right: 20px;
      border: 1px solid #eee;
      text-align: center;
      padding-bottom: 10px;
    }

    h3 {
      font-size: 15px;
      font-weight: normal;
      border-bottom: 1px solid #eee;
      padding: 10px;
      margin: 0;
    }

    ul {
      margin: 0;
      padding: 0;
      margin-left: 15px;
    }

    ul li {
      label {
        width: 5px;
        height: 5px;
        display: inline-block;
        background: #ccc;
        vertical-align: middle;
        margin-right: 10px;
      }
      display: flex;
      align-items: center;
      padding: 5px;
      list-style: none;
      cursor: pointer;
      font-size: 14px;
      color: #606266;
      text-align: left;
    }
    // 高亮
    .active {
      color: #1890ff;
      label {
        background: #1890ff;
      }
    }
  }
}
</style>
